package com.xh.easyretry.listener;


import com.xh.easyretry.annotations.XHRetry;
import com.xh.easyretry.common.XHRetryContext;
import com.xh.easyretry.common.XHRetryResult;

import java.util.Objects;

public class LastFailedListener<T extends XHRetryResult> extends RetryListener<T> {

    @Override
    public void listen(T t) {
        XHRetryContext retryContext = t.getRetryContext();
        Integer currentRetryCountRecord = t.getCurrentRetryCountRecord();
        XHRetry xhRetry = retryContext.getXhRetry();
        int maxCurrentRetryCount = xhRetry.retryCount();

        if (Objects.isNull(currentRetryCountRecord)) {
            return;
        }
        if (currentRetryCountRecord < maxCurrentRetryCount) {
            return;
        }

        String methodName = xhRetry.lastFailedListener();
        doWithSpecifyMethod(retryContext, methodName);
    }
}
